/*
    ShengDao Android Client, NToast
    Copyright (c) 2014 ShengDao Tech Company Limited
 */

package com.lzt.yijian.util;

import android.app.Application;
import android.text.TextUtils;
import android.widget.Toast;

import java.lang.ref.WeakReference;


public class NToast {

    private static Toast toast = null;  // Global Toast
    private static WeakReference<Application> app;

    public static void init(Application application) {
        app = new WeakReference<Application>(application);
    }

    public static Toast getToast() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
        toast = Toast.makeText(app.get(), "", Toast.LENGTH_SHORT);
        return toast;
    }

    public static void shortToast(int resId) {
        showToast(resId, Toast.LENGTH_SHORT);
    }

    public static void shortToast(String text) {
        if (!TextUtils.isEmpty(text) && !"".equals(text.trim())) {
            showToast(text, Toast.LENGTH_SHORT);
        }
    }

    public static void longToast(int resId) {
        showToast(resId, Toast.LENGTH_LONG);
    }

    public static void longToast(String text) {
        if (!TextUtils.isEmpty(text) && !"".equals(text.trim())) {
            showToast(text, Toast.LENGTH_LONG);
        }
    }

    public static void showToast(int resId, int duration) {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
        toast = Toast.makeText(app.get(), resId, duration);
        toast.show();
    }

    public static void showToast(String text, int duration) {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
        toast = Toast.makeText(app.get(), text, duration);
        toast.show();
    }

}
